package com.vivebest.utils.common.constant.enums;

import com.vivebest.utils.common.constant.MsgConstants;
import com.vivebest.utils.common.exception.BizException;
import lombok.Getter;

/**
 * <p>
 * 命令枚举
 * </p>
 *
 * @author zsk
 * @date 2020/8/12 上午11:11
 */
@Getter
public enum CmdEnums {
    /**
     * 解压tar
     */
    UNCOMPRESS_TAR("00001", "tar -xvf ${file} -C ${workPath}"),
    /**
     * 创建docker镜像
     */
    CREATE_DOCKER_IMAGE("00002", "docker build -t ${tag} ${workPath}"),
    /**
     * 推送docker镜像
     */
    PUSH_DOCKER_IMAGE("00003", "docker push ${tag}"),
    /**
     * 创建configmap
     */
    CREATE_CONFIG_MAP("00004", "kubectl create configmap ${configMapName} --from-file=${configFilePath} -n ${namespace}"),
    /**
     * 创建pod
     */
    CREATE_K8S_POD("00005", "kubectl create -f ${podFile}"),
    /**
     * 创建service
     */
    CREATE_K8S_SERVICE("00006", "kubectl create -f ${serviceFile}"),
    /**
     * 删除configmap
     */
    DELETE_CONFIG_MAP("00007", "kubectl delete configmap ${configMapName} -n ${namespace}"),
    /**
     * 删除pod
     */
    DELETE_K8S_POD("00008", "kubectl delete -f ${podFile}"),
    /**
     * 删除service
     */
    DELETE_K8S_SERVICE("00009", "kubectl delete -f ${serviceFile}"),
    /**
     * 获取consul所有服务注册名称
     */
    CONSUL_CATALOG_SERVICES("00010", "kubectl exec -f ${serviceFile} -- consul catalog services"),
    /**
     * 根据服务注册名称获取consul健康注册信息
     */
    CONSUL_WATCH_CHECKS_SERVICE("00011", "kubectl exec -f ${serviceFile} -- consul watch -type=checks -service=${serviceName}"),
    /**
     * 替换文件内容
     */
    SED("00012","sed -i 's/${oldStr}/${newStr}/' ${optFile}"),
    /**
     * 删除目录
     */
    RM("00013","rm -rf ${filePath}"),
    /**
     * 解药zip
     */
    UNCOMPRESS_ZIP("00014","unzip ${file} -d ${workPath}"),
    /**
     * 更改换行符
     */
    CHANGE_SEPARATOR("00014","sed -i 's/\r//' ${file}");
    /**
     * 命令代码
     */
    private String cmdCode;
    /**
     * 命令模板内容
     */
    private String cmdTmp;

    CmdEnums(String cmdCode, String cmdTmp) {
        this.cmdCode = cmdCode;
        this.cmdTmp = cmdTmp;
    }

    /**
     * 根据命令代码获取命令模板内容
     *
     * @param cmdCode 命令代码
     * @return 命令模板内容
     */
    public static String getCmdTmpByCmdCode(String cmdCode) {
        for (CmdEnums cmdEnums : CmdEnums.values()) {
            if (cmdEnums.getCmdCode().equals(cmdCode)) {
                return cmdEnums.getCmdTmp();
            }
        }
        throw new BizException(MsgConstants.COMM100002, cmdCode);
    }

    /**
     * 根据命令代码获取命令名称
     *
     * @param cmdCode 命令代码
     * @return 命令模板内容
     */
    public static String getCmdNameByCmdCode(String cmdCode) {
        for (CmdEnums cmdEnums : CmdEnums.values()) {
            if (cmdEnums.getCmdCode().equals(cmdCode)) {
                return cmdEnums.name();
            }
        }
        throw new BizException(MsgConstants.COMM100002, cmdCode);
    }
}
